//package com.ccb.sc.conf;
//
//import com.google.common.base.Predicate;
//import com.google.common.collect.Lists;
//import com.ccb.sc.constant.AppConst;
//import org.springframework.boot.actuate.autoconfigure.endpoint.web.CorsEndpointProperties;
//import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointProperties;
//import org.springframework.boot.actuate.autoconfigure.web.server.ManagementPortType;
//import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
//import org.springframework.boot.actuate.endpoint.web.*;
//import org.springframework.boot.actuate.endpoint.web.annotation.ControllerEndpointsSupplier;
//import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpointsSupplier;
//import org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Profile;
//import org.springframework.core.env.Environment;
//import org.springframework.util.StringUtils;
//import springfox.documentation.RequestHandler;
//import springfox.documentation.builders.ApiInfoBuilder;
//import springfox.documentation.builders.ParameterBuilder;
//import springfox.documentation.builders.PathSelectors;
//import springfox.documentation.schema.ModelRef;
//import springfox.documentation.service.*;
//import springfox.documentation.spi.DocumentationType;
//import springfox.documentation.spi.service.contexts.SecurityContext;
//import springfox.documentation.spring.web.plugins.Docket;
//import springfox.documentation.swagger2.annotations.EnableSwagger2;
//
//import java.util.ArrayList;
//import java.util.Collection;
//import java.util.List;
//import java.util.Optional;
//import java.util.function.Function;
//
///**
// * @author ken
// * @version 1.0
// * @date 2020-09-14
// */
//@Configuration
//@EnableSwagger2
//@Profile(value = {"dev", "test"})
//public class Swagger2Config {
//
//    @Bean
//    Docket createAppApi() {
//        return new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(
//                        new ApiInfoBuilder()
//                                .title("web-app接口")
//                                .description("App端接口文档")
//                                .termsOfServiceUrl("")
//                                .contact(new Contact("psl", "", "472506263@qq.com"))
//                                .version("1.0")
//                                .build()
//                ).groupName("App端接口")
//                .select()
//                .apis(regexBasePackage("com.ccb.sc.\\w+.controller;"))
//                .paths(PathSelectors.ant("/app/**"))
//                .build()
//                .globalOperationParameters(setHeaderToken())
//                .securitySchemes(securitySchemes())
//                .securityContexts(securityContexts())
//                ;
//    }
//
//    public static Predicate<RequestHandler> regexBasePackage(final String basePackage) {
//        return input -> declaringClass(input).map(handlerPackage(basePackage)).orElse(true);
//    }
//
//    /**
//     * 处理包路径配置规则,支持多路径扫描匹配以逗号隔开
//     *
//     * @param basePackage 扫描包路径
//     * @return Function
//     */
//    private static Function<Class<?>, Boolean> handlerPackage(final String basePackage) {
//        return input -> {
//            for (String packageStr : basePackage.split(";")) {
//                String packageName = input.getPackage().getName();
//                if (packageName.matches(packageStr)) {
//                    return true;
//                }
//            }
//            return false;
//        };
//    }
//
//    /**
//     * @param input RequestHandler
//     * @return Optional
//     */
//    private static Optional<Class<?>> declaringClass(RequestHandler input) {
//        return Optional.ofNullable(input.declaringClass());
//    }
//
//
//    private List<Parameter> setHeaderToken() {
//        List<Parameter> pars = new ArrayList<>();
//
//        // token请求头
//        String testTokenValue = "";
//
//        ParameterBuilder tokenPar = new ParameterBuilder();
//        Parameter tokenParameter = tokenPar
//                .name(AppConst.TOKEN_HEAD_NAME)
//                .description("Token Request Header")
//                .modelRef(new ModelRef("string"))
//                .parameterType("header")
//                .required(false)
//                .defaultValue(testTokenValue)
//                .build();
//        pars.add(tokenParameter);
//        return pars;
//    }
//
//    private List<SecurityScheme> securitySchemes() {
//        return Lists.newArrayList(new ApiKey("Authorization", AppConst.TOKEN_HEAD_NAME, "header"));
//    }
//
//    private List<SecurityContext> securityContexts() {
//        return Lists.newArrayList(SecurityContext.builder()
//                .securityReferences(defaultAuth())
//                //.forPaths(PathSelectors.regex("^(?!login).*$"))
//                .build());
//    }
//
//    private List<SecurityReference> defaultAuth() {
//        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
//        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
//        authorizationScopes[0] = authorizationScope;
//        return Lists.newArrayList(new SecurityReference("Authorization", authorizationScopes));
//    }
//
//    /**
//     * 增加如下配置可解决Spring Boot 6.x 与Swagger 3.0.0 不兼容问题
//     **/
//    @Bean
//    public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier, ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes, CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties, Environment environment) {
//        List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
//        Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
//        allEndpoints.addAll(webEndpoints);
//        allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
//        allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
//        String basePath = webEndpointProperties.getBasePath();
//        EndpointMapping endpointMapping = new EndpointMapping(basePath);
//        boolean shouldRegisterLinksMapping = this.shouldRegisterLinksMapping(webEndpointProperties, environment, basePath);
//        return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes, corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath), shouldRegisterLinksMapping, null);
//    }
//
//    private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties, Environment environment, String basePath) {
//        return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath) || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
//    }
//}
